//
//  SetDateTimeCell.m
//  QRMaster
//
//  Created by myApple on 15/7/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "SetDateTimeCell.h"

@interface SetDateTimeCell()
{
    UILabel * beginAndEndLabel;
    UILabel * weekInfoLabel;
    UISwitch * timeSwitch;
    NSArray * weekData;
}

@end

@implementation SetDateTimeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self prepareView];
        weekData = @[NSLocalizedString(@"Sunday", nil),NSLocalizedString(@"Monday", nil),NSLocalizedString(@"Tuesday", nil),NSLocalizedString(@"Wednesday", nil),NSLocalizedString(@"Thursday", nil),NSLocalizedString(@"Friday", nil),NSLocalizedString(@"Saturday", nil)];
    }
    return self;
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 16;
    float y = 10;
    float width = ScreenWidth - x - 80;
    float height = 20;

    beginAndEndLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    beginAndEndLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:beginAndEndLabel];
    
    y += height;
    height = 15;
    width = ScreenWidth - x - 60;
    weekInfoLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    weekInfoLabel.font = [UIFont systemFontOfSize:11.0];
    weekInfoLabel.textColor = RGBA(143, 147, 148, 1);
    [self.contentView addSubview:weekInfoLabel];
    
    
    x = ScreenWidth - 67;
    y = 12;
    height = 31;
    width = 51;
    timeSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeSwitch.on = NO;
    [timeSwitch addTarget:self action:@selector(swipSwitch) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:timeSwitch];
}

-(void)setBeginTime:(NSString *)beginTime
{
    _beginTime = beginTime;
}

-(void)setEndTime:(NSString *)endTime
{
    _endTime = endTime;
    beginAndEndLabel.text = [NSString stringWithFormat:@"%@~%@",_beginTime,_endTime];
}

-(void)setWeekInfo:(NSString *)weekInfo
{
    _weekInfo = weekInfo;
    NSArray * array = [weekInfo componentsSeparatedByString:@","];
    NSArray * sortArray = [self sortArray:array];
    NSMutableString * weekString = [[NSMutableString alloc]init];
    for (int i = 0 ; i < sortArray.count; i ++) {
        [weekString appendString:weekData[[sortArray[i] integerValue]]];
        [weekString appendString:@" "];
    }
    weekInfoLabel.text = weekString;
}

-(void)setOpenTime:(BOOL)openTime
{
    _openTime = openTime;
    if (_openTime) {
        timeSwitch.on = YES;
    }
    else
    {
        timeSwitch.on = NO;
    }
}

-(void)setCellIndext:(NSInteger)cellIndext
{
    _cellIndext = cellIndext;
}

-(void)swipSwitch
{

    if (_delegate && [_delegate respondsToSelector:@selector(swipSwitch:index:)]) {
        [_delegate swipSwitch:timeSwitch.on index:_cellIndext];
    }
}

-(void)setShowSwitch:(BOOL)showSwitch
{
    if (!showSwitch) {
        timeSwitch.hidden = YES;
    }
}


-(NSArray *)sortArray:(NSArray *)array
{
    NSArray * newArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1,NSString * obj2)
                          {
                             return [obj1 compare:obj2 options:NSNumericSearch];
                          }];
    return newArray;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
